﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FanPages.Server.Services.Contracts;
using FanPages.Server.Repostory.EFRepository.Contracts;
using FanPages.Server.Repostory.EFRepository;
using FanPages.Common;
using FanPages.Server.Repostory.EFRepository.Model;
using FanPages.Server.Services.Contracts.Dto;
using FanPages.Server.Services.Transformers;
using FanPages.Common.Exceptions;
using FanPages.Common.DataAccess;

namespace FanPages.Server.Services
{
    public class FanItemService : IFanItemService
    {
        IRepository<FanItem> repository = new EFRepository<FanItem>(new FanPagesEntities());

        public void Save(Contracts.Dto.FanItemDto fanItem)
        {
            try
            {                                
                IServerTransformer<FanItem, FanItemDto> transformer = new FanItemTransformerSvc();
                if (fanItem.Id != 0)
                {
                    var item = repository.First(x => x.Id == fanItem.Id);
                    repository.Update(transformer.ToExistingClient(fanItem, item));
                }
                else
                    repository.Add(transformer.ToClient(fanItem)); 
            }
            catch (Exception ex) 
            {
                throw new ServiceException("FanItemService Exception", ex);
            }

        }

        public List<FanItemDto> GetAll()
        {
            try
            {
                ITransformer<FanItem, FanItemDto> transformer = new FanItemTransformerSvc();
                var fanItems = repository.GetAll();
                var dtos = new List<FanItemDto>();
                foreach (var item in fanItems)
                {
                    dtos.Add(transformer.ToDto(item));
                }
                return dtos;
            }
            catch (Exception ex)
            {
                throw new ServiceException("FanItemService Exception", ex);
            }
        }


        public FanItemDto GetById(int id)
        {
            try
            {
                ITransformer<FanItem, FanItemDto> transformer = new FanItemTransformerSvc();
                var fanItem = transformer.ToDto(repository.First(x => x.Id == id));
                return fanItem;
            }
            catch (Exception ex)
            {
                throw new ServiceException("FanItemService.GetById Exception", ex);
            }
        }

        public void Delete(FanItemDto fanItem)
        {
            try
            {
                repository.Delete(repository.First(x=> x.Id == fanItem.Id));
            }
            catch (Exception ex)
            {
                throw new ServiceException("FanItemService.Delete Exception", ex);
            }
        }


        public List<FanItemDto> SearchByName(string description)
        {
            try
            {
                ITransformer<FanItem, FanItemDto> transformer = new FanItemTransformerSvc();
                var fanItems = repository.Find(x => x.Name.Contains(description));
                var dtos = new List<FanItemDto>();
                foreach (var item in fanItems)
                {
                    dtos.Add(transformer.ToDto(item));
                }
                return dtos;
            }
            catch (Exception ex)
            {
                throw new ServiceException("FanItemService.SearchByName Exception", ex);
            }
        }


        public FanItemDto GetByCode(string code)
        {
            try
            {
                ITransformer<FanItem, FanItemDto> transformer = new FanItemTransformerSvc();
                var fanItem = transformer.ToDto(repository.First(x => x.Code == code));
                return fanItem;
            }
            catch (Exception ex)
            {
                throw new ServiceException("FanItemService.GetById Exception", ex);
            }
        }
    }
}
